package com.lixinlei.tech.common.impl;

import com.lixinlei.tech.common.Codec;
import lombok.extern.slf4j.Slf4j;
import org.xerial.snappy.Snappy;

import java.io.IOException;

/**
 * snappy 编解码器实现
 */
@Slf4j
public class SnappyCodec implements Codec {

    @Override
    public byte[] encode(byte[] content) {
        if (content == null || content.length == 0) {
            return content;
        }
        try {
            return Snappy.compress(content);
        } catch (IOException e) {
            log.error("qunhe SnappyCodec encode error", e);
            return new byte[0];
        }
    }

    @Override
    public byte[] decode(byte[] content) {
        if (content == null || content.length == 0) {
            return content;
        }
        try {
            return Snappy.uncompress(content);
        } catch (IOException e) {
            log.error("qunhe SnappyCodec decode error", e);
            return new byte[0];
        }
    }
}
